<?php
class Crawl_netease_keyword_from_html_library  extends Crawl_keyword_library{

    private $_parameters = '';
    public  $_platform;
    public  $_htmlsearch_config;
    /**
     * 定义 CURL 消息头、用户端信息、抓取地址
     * 定义 新浪网页抓取选项
     */
    function __construct(){
        $this->_ci = & get_instance();
        $this->_ci->config->load('openplatforms', TRUE);
        $openplatforms = $this->_ci->config->item('openplatforms');
        $this->_htmlsearch_config  = $openplatforms['openplatforms']['htmlsearch']['netease'];
        $this->_null_tab    = $this->_htmlsearch_config['null'];
        $this->_platform    = $this->_ci->input->post('platform');
        $this->_url         = $this->_htmlsearch_config['url'];
        $this->_log_dir     = $this->_log_dir.'/'.$this->_ci->input->post('platform');

        $this->write_log_stary_tag();
    }


    public function crawl($retry = FALSE) {

        $crawl_result = $this->try_crawl($retry);

        if(is_array($crawl_result) && array_key_exists($this->_error_string, $crawl_result)) {
            return $crawl_result;
        } elseif(is_null($crawl_result)) {
            return array('data' => NULL);
        }
        return $this->make_data($crawl_result);
    }

    private function make_data($result = NULL) {
        $this->_ci->load->model('split/htmlsearchparser_model');

        $split_data = $this->_ci->htmlsearchparser_model->parser_netease($this->_htmlsearch_config, $result);
        if($split_data == FALSE) {
            log_message(self::LOG_LEVEL_WARING, 'Match And Split Data Failed!', FALSE, $this->_log_dir);
        }

        return $split_data;
    }

    /**
     * 尝试抓取
     * @param bool $retry 是否重试
     * @return array|bool 抓取成功返回数组
     */
    private function try_crawl($retry = FALSE) {
        $parameters = $this->make_parameters();
        if($retry == FALSE) {
            //不使用代理、不重试直接抓取
            log_message(self::LOG_LEVEL_NOTICE, 'Will Crawl From Netease Html Un Use Retry And Un Use Proxy With Parameters: '.json_encode($parameters), FALSE, $this->_log_dir);
            return self::get_content($parameters);
        } else {
            //$crawl_use_proxy_ip_result = $this->crawl_use_proxy_ip($this->_parameters, self::PRROXY_IP, $this->_crawl_from['netease_html'], FALSE);
            //if(is_array($crawl_use_proxy_ip_result) && array_key_exists($this->_error_string, $crawl_use_proxy_ip_result)) {
                return $this->crawl_use_proxy_ip($this->_parameters, self::MOBILITY_IP, $this->_crawl_from['netease_html'], FALSE);
            //} else {
                //return $crawl_use_proxy_ip_result;
            //}
        }
    }

    public function process_crawl_result($crawl_result, $use_proxy = self::PRROXY_IP) {
        if(is_array($crawl_result) && array_key_exists($this->_error_string, $crawl_result)) {
            return $crawl_result;
        }

        $crawl_result_array = $this->change_code($crawl_result['result']);
        if($crawl_result_array == FALSE) {
            $error = $this->_ci->error_code_library->getError('system', 'data', 'decode', $crawl_result);
            log_message(self::LOG_LEVEL_NOTICE, $error, FALSE, $this->_log_dir);
            $this->label_proxy_table($crawl_result['use_ip'], $use_proxy);
            return $error;
        }
        if(!array_key_exists('list', $crawl_result_array)){
            $error = $this->_ci->error_code_library->getError('system', 'data', 'return_error', json_encode($crawl_result));
            log_message(self::LOG_LEVEL_WARING, $error, FALSE, $this->_log_dir);
            return $error;
        }
        if(count($crawl_result_array['list']) < 1) {
            return NULL;
        }
        return $crawl_result_array['list'];
    }

    private function make_parameters() {
        $data = array(
            "k"         => trim($this->_ci->input->post('word')),
            "o"         =>"",
            "t"         =>"ALL",
            "ta"        =>"al",
            "pageNo"    =>$this->get_page(),
            "pageMethod"=>"page"
        );
        $parameters['url'] = $this->_url;
        $parameters['method'] = self::METHOD_POST;
        $parameters['postData'] = $data;

        return $this->_parameters = $parameters;
    }

}